package com.leo;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @ClassName ToggleButtonDemo
 * @Description Timeline 时间轴动画
 * @Author wangss
 * @date 2020.07.07 00:19
 * @Version 1.0
 */
public class Timeline4Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox(50);
        ToggleGroup tg = new ToggleGroup();

        ToggleButton tb1 = new ToggleButton("播放");
        tb1.setToggleGroup(tg);

        ToggleButton tb2 = new ToggleButton("暂停");
        tb2.setToggleGroup(tg);

        ToggleButton tb3 = new ToggleButton("停止");
        tb3.setToggleGroup(tg);

        ToggleButton tb4 = new ToggleButton("跳转");
        tb4.setToggleGroup(tg);

        // 旋转
//        Rotate rotate = new Rotate(0,0,0);
        // 缩放
        Scale scale = new Scale(1, 1, 0, 0);
        // 将正方形的中心改变
        Rectangle rec = new Rectangle(100, 100, Color.RED);
        rec.getTransforms().add(scale);

        KeyValue kv1X = new KeyValue(scale.xProperty(), 1);
        KeyValue kv1Y = new KeyValue(scale.yProperty(), 1);
        KeyFrame kf1 = new KeyFrame(Duration.seconds(0), kv1X, kv1Y);

        KeyValue kv2X = new KeyValue(scale.xProperty(), 2);
        KeyValue kv2Y = new KeyValue(scale.yProperty(), 2);
        KeyFrame kf2 = new KeyFrame(Duration.seconds(2), kv2X, kv2Y);

        Timeline timeline = new Timeline();
        timeline.getKeyFrames().addAll(kf1, kf2);
        // 无线循环 + 反向转圈
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);

        hBox.getChildren().addAll(tb1, tb2, tb3, tb4);
        AnchorPane an = new AnchorPane();
        an.getChildren().addAll(hBox, rec);
        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        AnchorPane.setTopAnchor(rec, 300d);
        AnchorPane.setLeftAnchor(rec, 100d);
        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ToggleButton");
        primaryStage.setWidth(1200d);
        primaryStage.setHeight(800d);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();

        addAction(timeline, tb1, tb2, tb3, tb4);
    }

    private void addAction(Timeline timeline, ToggleButton tb1, ToggleButton tb2, ToggleButton tb3, ToggleButton tb4) {
        tb1.setOnAction(event -> {
            timeline.play();
        });

        tb2.setOnAction(event -> {
            timeline.pause();
        });

        tb3.setOnAction(event -> {
            timeline.stop();
        });

        tb4.setOnAction(event -> {

        });
    }
}
